#include "stdafx.h"
#include "USpaceFillingContainerWindow.h"
#include "UWindowThemer.h"

using namespace std;

TSpaceFillingContainerWindow::TSpaceFillingContainerWindow(TRectangle Rect, TContainerWindow* parent, TWindowThemer *wt, std::string name)
	: TContainerWindow(Rect, parent, wt, name)
{

}

void TSpaceFillingContainerWindow::Draw( )
{
	for (unsigned int i=0; i < Children.size(); ++i)
	{
// 		int color2 = Children[i]->BorderColor;
// 		glColor3ub((color2 & 0xFF0000) >>16, (color2 & 0x00FF00)>>8, color2 & 0x0000FF);
// 		glBegin(GL_QUADS);
// 		glVertex2i(Left+((Right-Left)/(Children.size()))*i	, Top+BorderTop);
// 		glVertex2i(Left+((Right-Left)/(Children.size()))*i	, Top+BorderTop + BorderStandard*5);
// 		glVertex2i(Left+((Right-Left)/(Children.size()))*(i+1), Top+BorderTop+ BorderStandard*5);
// 		glVertex2i(Left+((Right-Left)/(Children.size()))*(i+1), Top+BorderTop);
// 		glEnd();
	}

	//TODO: Draw tabs, make sure the activechildindex'ed tab is the "active" tab

	Children[ActiveChildindex]->Draw();

	TContainerWindow::Draw();
}

void TSpaceFillingContainerWindow:: MouseMove(int X, int Y, int mstate)
{
	if (Children[ActiveChildindex]->Contains(X, Y))
		Children[ActiveChildindex]->MouseMove(X, Y, mstate);
	else
		TContainerWindow::MouseMove(X, Y, mstate);

}

void TSpaceFillingContainerWindow:: MouseDown(int X, int Y, uint8 button)
{
 	if ((Y<Children[ActiveChildindex]->Top-windowthemer->BorderTheme()->BorderTop())&& 
		(Y>Top))
 	{
 		ActiveChildindex = (X-Left) / (((Right-Left)/Children.size())+1);
 	}

	if (Children[ActiveChildindex]->Contains(X,Y))
		Children[ActiveChildindex]->MouseDown(X, Y, button);
	else
		TContainerWindow::MouseDown(X, Y, button);
}

void TSpaceFillingContainerWindow:: MouseUp(int X, int Y, uint8 button)
{
	if (Children[ActiveChildindex]->Contains(X,Y))
		Children[ActiveChildindex]->MouseUp(X, Y, button);
	else
		TContainerWindow::MouseUp(X, Y, button);
}

void TSpaceFillingContainerWindow::SetPos(TRectangle Rect)
{
	TDockable::SetPos(Rect);

	for (unsigned int i=0; i < Children.size(); ++i)
	{
		(Children[i]->SetPos(TRectangle(Left, Top+2*windowthemer->BorderTheme()->BorderTop(), Right, Bottom)));
	}
}

void TSpaceFillingContainerWindow::RemoveChild(TDockable* Child)
{
	Child->SetPos(TRectangle(Left, Top, Right, Bottom));
	TContainerWindow::RemoveChild(Child);

	if (Children.size()==1)
	{
		Parent->AddChild(Children[0]);
		Parent->RemoveChild(this);
		Children[0]->SetPos(TRectangle(Left, Top, Right, Bottom));
	}


}
